<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<mat-card *ngIf="healthCheckForm" [formGroup]="healthCheckForm">
  <mat-card-content class="health-check-card">
    <!-- Inherit configuration -->
    <gio-form-slide-toggle *ngIf="inheritHealthCheck" class="health-check-card__inherit-toggle">
      <gio-form-label>Inherit configuration</gio-form-label>
      <mat-slide-toggle
        gioFormSlideToggle
        formControlName="inherit"
        aria-label="Inherit configuration toggle"
        name="inheritHealthCheck"
      ></mat-slide-toggle>
    </gio-form-slide-toggle>

    <!-- Enable health-check -->
    <gio-form-slide-toggle class="health-check-card__enable-toggle">
      <gio-form-label>Enable health-check</gio-form-label>
      <mat-slide-toggle
        gioFormSlideToggle
        formControlName="enabled"
        aria-label="Health-check enable toggle"
        name="enableHealthCheck"
      ></mat-slide-toggle>
    </gio-form-slide-toggle>

    <mat-divider></mat-divider>

    <ng-container *ngIf="inheritHealthCheck">
      <gio-banner-info
        *ngIf="inheritHealthCheck.enabled && healthCheckForm.get('enabled').value && healthCheckForm.get('inherit').value"
        class="health-check-card__banner"
      >
        Inherited configuration preview from global health-check settings.
      </gio-banner-info>

      <gio-banner-info
        *ngIf="healthCheckForm.get('inherit').value && !healthCheckForm.get('enabled').value"
        class="health-check-card__banner"
      >
        No global health-check settings defined. When defined, they will be inherited.
      </gio-banner-info>

      <gio-banner-info
        *ngIf="!healthCheckForm.get('inherit').value && !healthCheckForm.get('enabled').value"
        class="health-check-card__banner"
      >
        Health-check configuration is disabled.
      </gio-banner-info>

      <gio-banner-info
        *ngIf="healthCheckForm.get('enabled').value && !healthCheckForm.get('inherit').value"
        class="health-check-card__banner"
      >
        Configure a specific health-check for this endpoint.
      </gio-banner-info>
    </ng-container>

    <div
      *ngIf="
        !inheritHealthCheck ||
        (inheritHealthCheck &&
          inheritHealthCheck.enabled &&
          healthCheckForm.get('inherit').value &&
          healthCheckForm.get('enabled').value) ||
        (inheritHealthCheck && !healthCheckForm.get('inherit').value && healthCheckForm.get('enabled').value)
      "
      [class.disabled]="isDisabled$ | async"
      class="health-check-card--forms"
    >
      <!-- Trigger -->
      <h3>Trigger</h3>
      <div class="health-check-card__trigger">
        <gio-form-cron class="health-check-card__trigger__field" formControlName="schedule">
          <gio-form-cron-label>Schedule *</gio-form-cron-label>
          <gio-form-cron-hint>Cron expression to schedule the health check</gio-form-cron-hint>
        </gio-form-cron>
      </div>
      <!-- Request -->
      <h3>Request</h3>
      <div class="health-check-card__request">
        <!-- Request HTTP Method -->
        <mat-form-field class="health-check-card__request__methods__form-field">
          <mat-label>HTTP Method</mat-label>
          <mat-select formControlName="method" required>
            <mat-option *ngFor="let method of httpMethods" [value]="method">{{ method }}</mat-option>
          </mat-select>
          <mat-error *ngIf="healthCheckForm.get('method').hasError('required')">HTTP Method is required.</mat-error>
        </mat-form-field>

        <!-- Request Path -->
        <mat-form-field class="health-check-card__request__path__form-field">
          <mat-label>Path</mat-label>
          <input type="text" matInput formControlName="path" required />
          <mat-hint>Additional path added to the endpoint target.</mat-hint>
          <mat-error *ngIf="healthCheckForm.get('path').hasError('required')">Path is required.</mat-error>
        </mat-form-field>

        <!-- Request fromRoot -->
        <gio-form-slide-toggle class="health-check-card__request__fromRoot__toggle">
          <gio-form-label>From root path ('/')</gio-form-label>
          <span class="mat-caption">
            When "from root" is enabled, the path is appended to the root path ('/') of the endpoint without any additional path configured
            at the endpoint level.
          </span>
          <mat-slide-toggle
            gioFormSlideToggle
            formControlName="fromRoot"
            aria-label="From root path enable toggle"
            name="fromRootCheck"
          ></mat-slide-toggle>
        </gio-form-slide-toggle>

        <!-- Request HTTP Body -->
        <mat-form-field
          *ngIf="healthCheckForm.get('method').value === 'POST' || healthCheckForm.get('method').value === 'PUT'"
          class="health-check-card__request__body__form-field"
        >
          <mat-label>Body</mat-label>
          <textarea
            aria-label="Endpoint target input"
            matInput
            formControlName="body"
            rows="5"
            maxlength="1000"
            placeholder="HTTP Body"
          ></textarea>
          <mat-hint>Make sure to add Content-Type header for POST/PUT HTTP method.</mat-hint>
        </mat-form-field>

        <!-- Request HTTP Headers -->
        <div class="health-check-card__request__http-headers">
          <div class="health-check-card__request__http-headers__label">HTTP Headers</div>
          <gio-form-headers class="health-check-card__request__http-headers__form-field" formControlName="headers"></gio-form-headers>
          <div class="health-check-card__request__http-headers__hint">
            Header value support EL (e.g: &#123; #dictionaries['my-dictionary']['my-property'] &#125;)
          </div>
        </div>
      </div>
      <!-- Assertions -->
      <h3>Assertions</h3>

      <div class="health-check-card__assertions" formArrayName="assertions">
        <gio-banner-info *ngIf="!(isDisabled$ | async)" class="health-check-card__assertions__banner">
          How to write assertion
          <span gioBannerBody>
            Example 1: Check the status of the HTTP response : <code>#response.status == 200</code><br />
            Example 2: Check the JSON content of the response <code>#jsonPath(#response.content, '$.status') == 'green'</code><br />
          </span>
        </gio-banner-info>
        <div
          *ngFor="let option of $any(healthCheckForm.get('assertions')).controls; let i = index"
          class="health-check-card__assertions__assertion"
        >
          <mat-form-field class="health-check-card__assertions__assertion__form-field">
            <mat-label>Assertion #{{ i }}</mat-label>
            <input type="text" matInput [formControlName]="i" required />
            <mat-error *ngIf="$any(healthCheckForm.get('assertions')).at(i).hasError('required')">Assertion is required.</mat-error>
          </mat-form-field>
          <button
            mat-icon-button
            class="health-check-card__assertions__assertion__remove-button"
            type="button"
            [disabled]="$any(healthCheckForm.get('assertions')).length === 1"
            (click)="removeAssertion(i)"
            matTooltip="Remove assertion"
            aria-label="Remove assertion"
          >
            <mat-icon svgIcon="gio:cancel"></mat-icon>
          </button>
        </div>

        <div class="health-check-card__assertions__add">
          <button mat-raised-button type="button" [disabled]="isDisabled$ | async" (click)="addAssertion()">
            <mat-icon svgIcon="gio:plus"></mat-icon> Add assertion
          </button>
        </div>
      </div>
    </div>
  </mat-card-content>
</mat-card>
